<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('合同表管理列表')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="btn-group-sm" role="group">
                <a class="btn btn-warning" onclick="$.table.exportWeekBudget()" shiro:hasPermission="system:contractDetail:export">
                    <i class="fa fa-download"></i> 导出合同表（周度预算）
                </a>
                <a class="btn btn-warning" onclick="$.table.exportWeekPredict()" shiro:hasPermission="system:contractDetail:export">
                    <i class="fa fa-download"></i> 导出合同表（周度预测）
                </a>
                <a class="btn btn-warning" onclick="$.table.exportWeekActual()" shiro:hasPermission="system:contractDetail:export">
                    <i class="fa fa-download"></i> 导出合同表（周度实际）
                </a>
                <a class="btn btn-warning" onclick="$.table.exportMonthBudget()" shiro:hasPermission="system:contractDetail:export">
                    <i class="fa fa-download"></i> 导出合同表（月度预算）
                </a>
                <a class="btn btn-warning" onclick="$.table.exportMonthPredict()" shiro:hasPermission="system:contractDetail:export">
                    <i class="fa fa-download"></i> 导出合同表（月度预测）
                </a>
                <a class="btn btn-warning" onclick="$.table.exportMonthActual()" shiro:hasPermission="system:contractDetail:export">
                    <i class="fa fa-download"></i> 导出合同表（月度实际）
                </a>
            </div>

            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <p>合同编号：</p>
                                <input type="text" name="contractNo"/>
                            </li>
                            <li>
                                <p>合同名称：</p>
                                <input type="text" name="contractName"/>
                            </li>
                            <li>
                                <p>销售负责人：</p>
                                <input type="text" name="xsPrincipalName"/>
                            </li>


                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>


            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:contractDetail:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:contractDetail:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:contractDetail:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <!--<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:contractDetail:export">-->
                    <!--<i class="fa fa-download"></i> 导出合同表-->
                 <!--</a>-->
                <!--<a class="btn btn-warning" onclick="$.table.exportExcelYingShou()" shiro:hasPermission="system:contractDetail:export">-->
                    <!--<i class="fa fa-download"></i> 导出应收账款表-->
                <!--</a>-->
                <!--<a class="btn btn-warning" onclick="$.table.exportExcelYingShou()" shiro:hasPermission="system:contractDetail:export">-->
                    <!--<i class="fa fa-download"></i> 导出应收账款表-->
                <!--</a>-->
                <a class="btn btn-success single" onclick="openMoneyBack()" shiro:hasPermission="system:contractDetail:moneyBack">
                    <i class="fa fa-bar-chart"></i> 回款管理
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:contractDetail:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:contractDetail:remove')}]];
        var prefix = ctx + "system/contractDetail";

        var prefixStage = ctx + "system/stage";
        var datas = [[${@dict.getType('sys_normal_disable')}]];
        var globalChildTables = new Array(); //记录所有展开的子表
        $(function() {
            var options = {
                url: prefix + "/list",
                striped : true, //是否显示行间隔色
                pageSize : 5,//单页记录数
                pageList : [ 5, 10, 20],//可选择单页记录数
                showRefresh : true,//刷新按钮
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                exportUrlWeekBudget: prefix + "/exportWeekBudget",
                exportUrlWeekPredict: prefix + "/exportWeekPredict",
                exportUrlWeekActual: prefix + "/exportWeekActual",
                exportUrlMonthBudget: prefix + "/exportMonthBudget",
                exportUrlMonthPredict: prefix + "/exportMonthPredict",
                exportUrlMonthActual: prefix + "/exportMonthActual",
                exportUrlYingShou: prefix + "/exportYingShou",

                modalName: "合同表",
                clickToSelect: true,
                detailView: true,
                onExpandRow : function(index, row, $detail) {
					initChildTable(index, row, $detail);
				},
                onCollapseRow : function(index, row, $detail){
                    globalChildTables.splice(index,1);
                },
                columns: [{
                    checkbox: true
                },
                {
                    field : 'contractId', 
                    title : '主键id',
                    visible: false
                },
                {
                    field : 'contractNo', 
                    title : '合同编号'
                },
                {
                    field : 'contractName', 
                    title : '合同名称'
                },
				 {
                    field : 'xsPrincipalName', 
                    title : '销售负责人'
                },
                {
                    field : 'partyAName', 
                    title : '甲方名称'
                },
                {
                    field : 'biddingTime', 
                    title : '中标时间',
                    formatter: function(value, row, index){
                        return changeDateFormat(value);
                    }
                },
                {
                    field : 'biddingInyear',
                    title : '中标年度'
                },

                {
                    field : 'xsDepartment', 
                    title : '销售二级部门'
                },
                {
                    field : 'contractSignTime', 
                    title : '合同签订时间',
                    formatter: function(value, row, index){
                        return changeDateFormat(value);
                    }
                },
                {
                    field : 'biddingMoney',
                    title : 'bidding金额'
                },
                {
                    field : 'booking',
                    title : 'booking金额'
                },
                    {
                        field : 'contractMoney',
                        title : '合同金额'
                    },
                    {
                        field : 'balanceSince2020',
                        title : '20年初合同余额'
                    },

                    {
                        field : 'hasReceivedMoney',
                        title : '已回款金额'
                    },
                    {
                        field : 'notReceivedMoney',
                        title : '未回款金额'
                    },
                    {
                        field : 'hasReceivedRate',
                        title : '已回款比例'
                    },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' 
                        + editFlag 
                        + '" href="javascript:void(0)" onclick="openChildTable(\'' + row.contractId + '\')"><i class="fa fa-edit"></i>添加阶段</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.contractId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.contractId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        var prefixSystem = ctx + "system";

        openChildTable = function(contractId) {
            $.modal.openOptions({
                title: '添加阶段',
                width: "680",
                url: prefixStage + "/add/" + contractId,
                callBack: function(index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                }
            });
        }
        initChildTable = function(index, row, $detail) {
            console.log(row);
			var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
            globalChildTables[index] = $(childTable);
    	    $(childTable).bootstrapTable({
    	        url: prefixStage + "/list",
    	        method: 'post',
                striped : true, //是否显示行间隔色
                pageNumber : 1, //初始化加载第一页
                pagination : false,//是否分页
                showRefresh : true,//刷新按钮
    	        contentType: "application/x-www-form-urlencoded",
    	        queryParams : {
                    contractId: row.contractId
				},
    	        columns: [
                {
                    field : 'stageId',
                    title : 'id',
                    visible: false
                },
                {
                    field : 'contractId',
                    title : '合同id',
                    visible: false
                },
                {
                    field : 'stageNo',
                    title : '阶段序号'
                },
                {
                    field : 'stageName',
                    title : '阶段名称'
                },
                {
                    field : 'stagePrincipal1',
                    title : '阶段负责人一'
                },
                {
                    field : 'stageMoney',
                    title : '本阶段合同收款金额'
                },
                {
                    field : 'budgetEndTime',
                    title : '销售计划结束时间',
                    formatter: function(value, row, index){
                        return changeDateFormat(value);
                    }
                },
                {
                    field : 'predictEndTime',
                    title : '实施预测结束时间',
                    formatter: function(value, row, index){
                        return changeDateFormat(value);
                    }
                },
                {
                    field : 'actualEndTime',
                    title : '交付实际结束时间',
                    formatter: function(value, row, index){
                        return changeDateFormat(value);
                    }
                },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="stageEdit(\'' + row.stageId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="stageRemove(\'' + row.stageId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            });

        };

        openChildBudgetTable = function(stageId) {
            $.modal.openOptions({
                title: '选择字典类型',
                width: "680",
                url: prefixSystem + "/budget/add/" + stageId,
                callBack: function(index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                }
            });
        }
        openChildPredictTable = function(stageId) {
            $.modal.openOptions({
                title: '选择字典类型',
                width: "680",
                url: prefixSystem + "/predict/add/" + stageId,
                callBack: function(index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                }
            });
        }
        openChildActualTable = function(stageId) {
            $.modal.openOptions({
                title: '选择字典类型',
                width: "680",
                url: prefixSystem + "/actual/add/" + stageId,
                callBack: function(index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                }
            });
        }
        openChildEditBudgetTable = function(stageId) {
            $.modal.openOptions({
                title: '选择字典类型',
                width: "1280",
                url: prefixSystem + "/budget/editbudget/" + stageId,
                callBack: function(index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                }
            });
        }
        openChildEditPredictTable = function(stageId) {
            $.modal.openOptions({
                title: '编辑预测收款列表',
                width: "1280",
                url: prefixSystem + "/predict/editpredict/" + stageId,
                callBack: function(index, layero) {
                    return true;
                }
            });
        }
        openChildEditActualTable = function(stageId) {
            $.modal.openOptions({
                title: '编辑实际收款列表',
                width: "1280",
                url: prefixSystem + "/actual/editactual/" + stageId,
                callBack: function(index, layero) {
                    close()
                }
            });
        }

        layui.use('laydate', function(){
            var laydate = layui.laydate;
            laydate.render({ 
                elem: '#laydate-demo-2',
                type: 'date'
            });
        
        });
        function stageEdit(stageId){
            $.modal.openOptions({
                title: '修改合同阶段',
                width: "680",
                url: prefixStage + "/edit/" + stageId,
                callBack: function(index, layero) {
                    var type = 0; //从合同页面编辑的stage
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero, type);
                    for(let i = 0; i < globalChildTables.length; i++){ //删除成功后，所有的子表刷新
                        globalChildTables[i].bootstrapTable('refresh');
                    }
                }
            });
        }
        function stageRemove(stageId){
            $.modal.confirm("确定删除该条合同阶段信息吗？", function() {
                var config = {
                    url: prefixStage + "/remove",
                    type: 'post',
                    dataType: 'json',
                    data: { "ids": stageId },
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                    },
                    success: function(result) {
                        if (result.code == web_status.SUCCESS) {
                            $.modal.msgSuccess(result.msg);
                            for(let i = 0; i < globalChildTables.length; i++){ //删除成功后，所有的子表刷新
                                globalChildTables[i].bootstrapTable('refresh');
                            }
                        } else if (result.code == web_status.WARNING) {
                            $.modal.alertWarning(result.msg)
                        }  else {
                            $.modal.alertError(result.msg);
                        }
                        $.modal.closeLoading();
                    }
                };
                $.ajax(config)
            });
        }
        function openMoneyBack() {
            var row = $('#bootstrap-table').bootstrapTable('getSelections');
            $.modal.openOptions({
                title: '回款管理-' + row[0].contractName + '(' + row[0].contractNo + ')',
                width: "1280",
                btn: ['<i class="fa fa-close"></i> 关闭'],
                url: prefix + "/moneyBack/" + row[0].contractId,
                callBack: function(index, layero) {
                    layer.close(index);
                }
            });
        }

        function changeDateFormat(cellval){
            var dateVal = cellval + "";
            if (cellval != null) {
                dateVal = dateVal.split(' ');
                return dateVal[0];
            } else {
                return '-';
            }
        }
    </script>
</body>
</html>